package com.crawler.db

import java.sql.{Connection, DriverManager}

import com.crawler.tables.Tables.profile.api._
import org.apache.commons.dbcp2.BasicDataSource
import org.slf4j.LoggerFactory

/**
  * Created by tongtao.zhu on 2016/11/24.
  */
object Datasource{
  val path = "classpath:task.sql"
  var connectionPool:BasicDataSource = null
  def getConnectionPool(dbName: String): BasicDataSource ={
    if (connectionPool == null){
      connectionPool = new BasicDataSource()
      val dbUrl = s"jdbc:sqlite:./db/$dbName.db"
      connectionPool.setDriverClassName("org.sqlite.JDBC")
      connectionPool.setUrl(dbUrl)
      connectionPool.setInitialSize(1)
      connectionPool.setMaxIdle(1)
      connectionPool.setMaxTotal(1)
    }
    connectionPool
  }

  def main(args: Array[String]) {
    val sql = """create table IF NOT EXISTS "TASK"(
      "METHOD" VARCHAR NOT NULL ,
      "DATA" VARCHAR NOT NULL ,
      "HOST" VARCHAR NOT NULL ,
      "PARAM" VARCHAR NOT NULL ,
      "HEADER" VARCHAR NOT NULL ,
      "SOURCE" VARCHAR NOT NULL,
      "TYPE"  VARCHAR NOT NULL,
      "STATUS" INT NOT NULL,
      "PRIOR" INT NOT NULL,
      "DATE" BIGINT NOT NULL,
      "MD5" VARCHAR NOT NULL
    );
    CREATE INDEX IF NOT EXISTS ind_md5 ON  TASK(MD5);"""
    val conn = getConnectionPool("TaoBao").getConnection
    conn.createStatement().execute(sql)
    conn.close()
  }
}
